package com.hyl.authdemo.controller;

import com.alibaba.fastjson.JSON;
import com.hyl.authdemo.entity.User;
import com.hyl.authdemo.entity.UserTokenVo;
import com.hyl.authdemo.service.TokenService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/7/6 14:39
 **/
@RestController
@RequestMapping(value = "/index")
@CrossOrigin(originPatterns = "http://localhost:8080")
public class IndexController {
	@Resource
	TokenService tokenService;

	@ApiOperation(value = "登入")
	@ApiImplicitParam(name = "username",value = "用户名",defaultValue = "admin",required = true)
	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public String login(@RequestParam(required = true) String username, String pwd){
		//验证用户是否登入成功
		//成功==》封装token==》返回数据
		UserTokenVo vo = tokenService.saveToken(username);
		return JSON.toJSONString(vo);
	}

	@RequestMapping(value = "/test")
	public String test(HttpServletRequest request, HttpServletResponse response){
//		response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080");

		//判断用户是否已经登入成功，并且获取登入用的信息
		String token = request.getHeader("token");
		//redis中的key
		boolean bool = tokenService.isExist(token);

		System.out.println("token："+bool);

		List<User> list = new ArrayList<User>();
		for(int i=0;i<10;i++){
			User tmp = new User("2022-12-12", "牛马"+i,"郴州");
			list.add(tmp);
		}
		return JSON.toJSONString(list);
	}

	public String sendPhone(){
		return "短信";
	}

	public String sendEmail(String to){
		return "短信";
	}
}
